﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// job_list 的摘要说明
    /// </summary>
    public class zhoubian_list : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        protected int page = 1;
        protected int pageSize = 0;
        protected int category_id = 0;
        public void ProcessRequest(HttpContext context)
        {
            this.pageSize = 15;
            this.page = DTRequest.GetQueryInt("page", 1);
            this.category_id = DTRequest.GetQueryInt("category_id", 0);

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.zhoubian bll = new BLL.zhoubian();
            DataSet ds = bll.GetList(this.category_id, this.pageSize, this.page, "", "id desc", out this.totalCount);
            int total_page = this.totalCount / this.pageSize;
            strTxt.Append("\"total_page\":" + total_page.ToString() + ",");

            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"zhoubian_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"category_id\":" + dr["category_id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    string imgurl = dr["img_url"].ToString();

                    if (string.IsNullOrEmpty(imgurl))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        if (imgurl.Contains("shicaiquanzi"))
                        {
                            strTxt.Append(",\"img_url\":\"http:\\/\\/" + imgurl + "\"");
                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http:\\/\\/www.shicaiquanzi.com" + imgurl + "\"");
                        }
                    }
                    
                    if (Convert.ToInt32(dr["user_id"]) > 0)
                    {
                        string user_name = new BLL.users().GetUserName(Convert.ToInt32(dr["user_id"]));
                        string _mobile = new BLL.users().GetMobile(Convert.ToInt32(dr["user_id"]));
                        strTxt.Append(",\"lianxiren\":\"" + user_name + "\"");
                        strTxt.Append(",\"mobile\":\"" + _mobile + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"lianxiren\":\"" + dr["lianxiren"] + "\"");
                        strTxt.Append(",\"mobile\":\"" + dr["mobile"] + "\"");
                    }
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append("\"zhoubian_list\":[]");
            }
            strTxt.Append("}");

            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}